<?php

//后台控制器
class Main extends Controller {

	var $viewpath;
	var $is_logined;
	
	function Main()
	{
		parent::Controller();
		$this->viewpath = 'admin/'.__CLASS__;
		$this->load->model('managers_model','mm');
		$this->is_logined = $this->mm->is_logined();
	}
	
	//后台默认首页
	function index()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$manager = $this->mm->get_account();
		$viewdata['viewpath'] = $this->viewpath;
		$viewdata['manager'] = $manager;
		$this->load->view($this->viewpath.'/index', $viewdata);
	}
	
	//常规内容输出
	function show()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$viewpage = "show_".$this->uri->segment(4);
		$this->load->view($this->viewpath.'/'.$viewpage);
	}
	
	//登录界面
	function login()
	{
		if($this->is_logined) {
			admin_show_failed("已经登录");
		} 
		if($dosubmit = p("dosubmit")) {
			//验证码
			$flag = $this->check_yzcode(p("yzcode"));
			if(!$flag) {
				admin_show_failed("验证码错误");
			}
			$logindata = array(
				"username" => p("username"),
				"password" => p("password"),
			);
			$result = $this->mm->login($logindata);
			if($result) {//提示成功
				admin_show_success("登陆成功", $this->config->item('admin_path').'/main');
			} else {
				admin_show_failed("登陆失败");
			}
		}
		$this->load->view($this->viewpath.'/login');
	}
	
	//退出登录
	function logout()
	{
		if(!$this->is_logined) {
			redirect($this->config->item('admin_path')."/main/login");
		} 
		$this->mm->logout();
		admin_show_success("退出成功", $this->config->item('admin_path').'/main/login');
	}
	
	//检测验证码
	function check_yzcode($yzcode) 
	{
		session_start();
		if($yzcode != $_SESSION["yzcode"]) {
			return false;
		}
		return true;
	}

}

/* End of file */